package cn.com.css.taxInspection.workflow.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.gt3.entity.JcAjxxEntity;
import cn.com.css.taxInspection.gt3.entity.JcSjlEntity;
import cn.com.css.taxInspection.gt3.service.JcAjxxService;
import cn.com.css.taxInspection.gt3.service.JcSjlService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.workflow.constants.WorkflowConstants;
import cn.com.css.taxInspection.workflow.entity.WorkflowActivityEntity;
import cn.com.css.taxInspection.workflow.entity.WorkflowInspectionHearEntity;
import cn.com.css.taxInspection.workflow.mapper.WorkflowActivityMapper;
import cn.com.css.taxInspection.workflow.service.WorkflowActivityService;
import cn.com.css.taxInspection.workflow.service.WorkflowInspectionHearService;

@Service("workflowActivityService")
public class WorkflowActivityServiceImpl extends ServiceImpl<WorkflowActivityMapper, WorkflowActivityEntity>
        implements WorkflowActivityService {

    @Resource
    private JcAjxxService jcAjxxService;
    @Resource
    private JcSjlService jcSjlService;
    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private WorkflowInspectionHearService workflowInspectionHearService;

    @Override
    public void saveByWorkflowActivity(WorkflowActivityEntity workflowActivityEntity) {

        workflowActivityEntity.setTaxorgcode(httpSessionService.getCurrentDeptId());
        this.save(workflowActivityEntity);
    }

    @Override
    @DS("oracle")
    public List<WorkflowActivityEntity> getGt3Workflow(String jcajxxuuid) {
        JcSjlEntity jcSjlEntity = jcSjlService.getById(jcajxxuuid);
        if (TsBqUtils.isNull(jcSjlEntity)) {
            return new ArrayList<WorkflowActivityEntity>();
        } else {
            List<WorkflowActivityEntity> list = new ArrayList<WorkflowActivityEntity>();
            Date larq = jcSjlEntity.getLarq();
            String lar = jcSjlEntity.getLar();
            if (TsBqUtils.isNotNull(larq)) {
                WorkflowActivityEntity activity1 = new WorkflowActivityEntity();
                activity1.setCreateId(lar);
                activity1.setCreateTime(larq);
                activity1.setStateName("立案日期");
                activity1.setRemark("金三流程");
                list.add(activity1);
            }
            Date zcrq = jcSjlEntity.getZcrq();
            String zcr = jcSjlEntity.getZcr();
            if (TsBqUtils.isNotNull(zcrq)) {
                WorkflowActivityEntity activity2 = new WorkflowActivityEntity();
                activity2.setCreateId(zcr);
                activity2.setCreateTime(zcrq);
                activity2.setStateName("检查科长分配检查人日期");
                activity2.setRemark("金三流程");
                list.add(activity2);
            }
            Date jtzfrq = jcSjlEntity.getJtzfrq();
            String jtzfr = jcSjlEntity.getJtzfr();
            if (TsBqUtils.isNotNull(jtzfrq)) {
                WorkflowActivityEntity activity3 = new WorkflowActivityEntity();
                activity3.setCreateId(jtzfr);
                activity3.setCreateTime(jtzfrq);
                activity3.setStateName("检通制作日期");
                activity3.setRemark("金三流程");
                list.add(activity3);
            }

            Date xhrq = jcSjlEntity.getXhrq();
            String xhr = jcSjlEntity.getXhr();
            if (TsBqUtils.isNotNull(xhrq)) {
                WorkflowActivityEntity activity4 = new WorkflowActivityEntity();
                activity4.setCreateId(xhr);
                activity4.setCreateTime(xhrq);
                activity4.setStateName("检通销号日期");
                activity4.setRemark("金三流程");
                list.add(activity4);
            }
            Date jcjsrq = jcSjlEntity.getJcjsrq();
            if (TsBqUtils.isNotNull(jcjsrq)) {
                WorkflowActivityEntity activity5 = new WorkflowActivityEntity();
                activity5.setCreateId("");
                activity5.setCreateTime(jcjsrq);
                activity5.setStateName("检查推送审理日期");
                activity5.setRemark("金三流程");
                list.add(activity5);
            }

            Date sldjrq = jcSjlEntity.getSldjrq();
            if (TsBqUtils.isNotNull(sldjrq)) {
                WorkflowActivityEntity activity6 = new WorkflowActivityEntity();
                activity6.setCreateId("");
                activity6.setCreateTime(sldjrq);
                activity6.setStateName("审理接收日期");
                activity6.setRemark("金三流程");
                list.add(activity6);
            }
            Date wszzrqCl = jcSjlEntity.getWszzrqCl();
            String wszzrCl = jcSjlEntity.getWszzrCl();
            if (TsBqUtils.isNotNull(wszzrqCl)) {
                WorkflowActivityEntity activity7 = new WorkflowActivityEntity();
                activity7.setCreateId(wszzrCl);
                activity7.setCreateTime(wszzrqCl);
                activity7.setStateName("处理决定书制作日期");
                activity7.setRemark("金三流程");
                list.add(activity7);
            }
            Date wszzrqCf = jcSjlEntity.getWszzrqCf();
            String wszzrCf = jcSjlEntity.getWszzrCf();
            if (TsBqUtils.isNotNull(wszzrqCf)) {
                WorkflowActivityEntity activity8 = new WorkflowActivityEntity();
                activity8.setCreateId(wszzrCf);
                activity8.setCreateTime(wszzrqCf);
                activity8.setStateName("处罚决定书制作日期");
                activity8.setRemark("金三流程");
                list.add(activity8);
            }
            Date sljsrq = jcSjlEntity.getSljsrq();
            if (TsBqUtils.isNotNull(sljsrq)) {
                WorkflowActivityEntity activity9 = new WorkflowActivityEntity();
                activity9.setCreateId("");
                activity9.setCreateTime(sljsrq);
                activity9.setStateName("审理推送执行时间");
                activity9.setRemark("金三流程");
                list.add(activity9);
            }
            Date zxdjrq = jcSjlEntity.getZxdjrq();
            if (TsBqUtils.isNotNull(zxdjrq)) {
                WorkflowActivityEntity activity10 = new WorkflowActivityEntity();
                activity10.setCreateId("");
                activity10.setCreateTime(zxdjrq);
                activity10.setStateName("执行接收日期");
                activity10.setRemark("金三流程");
                list.add(activity10);
            }
            Date xhrqCl = jcSjlEntity.getXhrqCl();
            String xhrCl = jcSjlEntity.getXhrCl();
            if (TsBqUtils.isNotNull(xhrqCl)) {
                WorkflowActivityEntity activity11 = new WorkflowActivityEntity();
                activity11.setCreateId(xhrCl);
                activity11.setCreateTime(xhrqCl);
                activity11.setStateName("处理决定书销号日期");
                activity11.setRemark("金三流程");
                list.add(activity11);
            }
            Date xhrqCf = jcSjlEntity.getXhrqCf();
            String xhrCf = jcSjlEntity.getXhrCf();
            if (TsBqUtils.isNotNull(xhrqCf)) {
                WorkflowActivityEntity activity12 = new WorkflowActivityEntity();
                activity12.setCreateId(xhrCf);
                activity12.setCreateTime(xhrqCf);
                activity12.setStateName("处罚决定书销号日期");
                activity12.setRemark("金三流程");
                list.add(activity12);
            }

            Date zxwbrq = jcSjlEntity.getZxwbrq();
            if (TsBqUtils.isNotNull(zxwbrq)) {
                WorkflowActivityEntity activity13 = new WorkflowActivityEntity();
                activity13.setCreateId("");
                activity13.setCreateTime(zxwbrq);
                activity13.setStateName("执行完毕时间");
                activity13.setRemark("金三流程");
                list.add(activity13);
            }

            Date jarq = jcSjlEntity.getJarq();
            if (TsBqUtils.isNotNull(jarq)) {
                WorkflowActivityEntity activity14 = new WorkflowActivityEntity();
                activity14.setCreateId("");
                activity14.setCreateTime(jarq);
                activity14.setStateName("结案日期");
                activity14.setRemark("金三流程");
                list.add(activity14);
            }
            return list;
        }
    }

    @Override
    @DS("oracle")
    public void lock(WorkflowInspectionHearEntity workflowInspectionHear) {
        Date date = new Date();
        String currentUserId = httpSessionService.getCurrentUserId();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        String jcajxxuuid = workflowInspectionHear.getJcajxxuuid();
        String stateCode;
        WorkflowInspectionHearEntity entity = workflowInspectionHearService.getById(jcajxxuuid);
        JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(jcajxxuuid);
        if (TsBqUtils.isNull(entity)) {
            // 初始化一条 workflowInspectionHear
            WorkflowInspectionHearEntity workflowInspectionHearEntity = new WorkflowInspectionHearEntity();
            workflowInspectionHearEntity.setId(jcajxxuuid);
            workflowInspectionHearEntity.setRelevance(jcajxxuuid);
            workflowInspectionHearEntity.setStateCode(WorkflowConstants.INSPECT_11);
            workflowInspectionHearEntity.setStateName(WorkflowConstants.INSPECT_11_NAME);
            String jcry = jcAjxxEntity.getJcry();
            workflowInspectionHearEntity.setInspectionMan11(jcry);
            workflowInspectionHearEntity.setInspectionPost11(WorkflowConstants.ROLE_1708113478976823298);
            workflowInspectionHearEntity.setInspectionTaxorg11(currentDeptId);
            workflowInspectionHearEntity.setDeleted(Constants.DELETEED_NOT);
            workflowInspectionHearEntity.setCreateTime(date);
            workflowInspectionHearEntity.setCreateId(currentUserId);
            workflowInspectionHearService.save(workflowInspectionHearEntity);
            stateCode = WorkflowConstants.INSPECT_99;
        } else {
            stateCode = entity.getStateCode();
        }
        WorkflowActivityEntity workflowActivityEntity = new WorkflowActivityEntity();
        workflowActivityEntity.setId(TsBqUtils.getUUID());
        workflowActivityEntity.setRelevance(jcajxxuuid);
        workflowActivityEntity.setCurrentMan(currentUserId);
        if (stateCode.equals(WorkflowConstants.INSPECT_11)) {
            workflowActivityEntity.setStateName(WorkflowConstants.ACTIVITY_1);
            workflowActivityEntity.setStateCode(WorkflowConstants.ACTIVITY_1_CODE);
        } else if (stateCode.equals(WorkflowConstants.INSPECT_12)) {
            workflowActivityEntity.setStateName(WorkflowConstants.ACTIVITY_2);
            workflowActivityEntity.setStateCode(WorkflowConstants.ACTIVITY_2_CODE);
        } else if (stateCode.equals(WorkflowConstants.HEAR_21)) {
            // 审理环节接收
            workflowActivityEntity.setStateName(WorkflowConstants.ACTIVITY_8);
            workflowActivityEntity.setStateCode(WorkflowConstants.ACTIVITY_8_CODE);
            // 更新 workflowInspectionHear
            entity.setStateCode(WorkflowConstants.HEAR_20);
            entity.setStateName(WorkflowConstants.HEAR_20_NAME);
            workflowInspectionHearService.updateById(entity);
        } else {
            workflowActivityEntity.setStateName(WorkflowConstants.ACTIVITY_0);
            workflowActivityEntity.setStateCode(WorkflowConstants.ACTIVITY_0_CODE);
        }
        workflowActivityEntity.setDeleted(Constants.DELETEED_NOT);
        workflowActivityEntity.setCreateTime(date);
        workflowActivityEntity.setCreateId(currentUserId);
        this.saveByWorkflowActivity(workflowActivityEntity);
    }
}